package com.ruoyi.ap.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.ap.pojo.vo.FarmerVo;
import com.ruoyi.ap.service.IVillageService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ap.domain.Farmer;
import com.ruoyi.ap.service.IFarmerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农户Controller
 * 
 * @author HuaQP
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/ap/farmer")
public class FarmerController extends BaseController
{
    @Resource
    private IFarmerService farmerService;
    @Resource
    private IVillageService villageService;

    /**
     * 查询农户列表
     */
    @PreAuthorize("@ss.hasPermi('ap:farmer:list')")
    @GetMapping("/list")
    public TableDataInfo listVo(FarmerVo farmerVo)
    {
        startPage();
        List<FarmerVo> list = farmerService.selectFarmerVoList(farmerVo);
        return getDataTable(list);
    }

    /**
     * 导出农户列表
     */
    @PreAuthorize("@ss.hasPermi('ap:farmer:export')")
    @Log(title = "农户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FarmerVo farmerVo)
    {
        List<FarmerVo> list = farmerService.selectFarmerVoList(farmerVo);
        ExcelUtil<FarmerVo> util = new ExcelUtil<>(FarmerVo.class);
        util.exportExcel(response, list, "农户数据");
    }

    /**
     * 获取农户详细信息
     */
    @PreAuthorize("@ss.hasPermi('ap:farmer:query')")
    @GetMapping(value ={"/", "/{id}"})
    public AjaxResult getInfo(@PathVariable(value = "id",required = false) Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("villages",villageService.selectVillageAll());
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, farmerService.selectFarmerById(id));
        }
        return ajax;
    }

    /**
     * 新增农户
     */
    @PreAuthorize("@ss.hasPermi('ap:farmer:add')")
    @Log(title = "农户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid Farmer farmer)
    {
        return toAjax(farmerService.insertFarmer(farmer));
    }

    /**
     * 修改农户
     */
    @PreAuthorize("@ss.hasPermi('ap:farmer:edit')")
    @Log(title = "农户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid Farmer farmer)
    {
        return toAjax(farmerService.updateFarmer(farmer));
    }

    /**
     * 删除农户
     */
    @PreAuthorize("@ss.hasPermi('ap:farmer:remove')")
    @Log(title = "农户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(farmerService.deleteFarmerByIds(ids));
    }

    /**
     * 获取农户树列表
     */
    @PreAuthorize("@ss.hasPermi('ap:farmer:list')")
    @GetMapping("/farmerTree")
    public AjaxResult farmerTree()
    {
        return success(farmerService.selectFarmerTreeList());
    }
}
